
/*
 * Copyright (C) Zhu Jiashun
 * Copyright (C) Zaver
 */

#ifndef ZV_TIMER_H
#define ZV_TIMER_H

#include "priority_queue.h"
#include "http_request.h"

#define ZV_TIMER_INFINITE -1
#define TIMEOUT_DEFAULT 500 /* 默认超时时间 50ms */

typedef int (*timer_handler_pt)(zv_http_request_t *rq);

typedef struct zv_timer_node_s
{
    size_t key;
    int deleted;              /* if remote client close the socket first, set deleted to 1 关闭超时回调 */
    timer_handler_pt handler; // 超时回调函数
    zv_http_request_t *rq;    // 请求
} zv_timer_node;

int zv_timer_init();            // 定时器初始化
int zv_find_timer();            // 寻找定时器节点
void zv_handle_expire_timers(); // 处理超时节点

extern zv_pq_t zv_timer;       // 定时器，基于优先队列
extern size_t zv_current_msec; // 当前时间

void zv_add_timer(zv_http_request_t *rq, size_t timeout, timer_handler_pt handler); // 加入定时节点
void zv_del_timer(zv_http_request_t *rq);                                           // 删除节点

#endif
